package com.vip.eshop.inventory.request;

import com.vip.eshop.inventory.model.ProductInventory;
import com.vip.eshop.inventory.service.ProductInventoryService;

/**
 * Date Update Operation
 * Created by leo.song on 2017/8/25.
 *
 * 1. delete cache
 * 2. update database table row.
 */
public class ProductUpdateRequest implements Request {

    private ProductInventory productInventory;
    private ProductInventoryService inventoryService;

    @Override
    public void process() {
        System.out.println("===========日志===========: 数据库更新请求开始执行，" +
                "商品id=" + productInventory.getProductId() +
                ", 商品库存数量=" + productInventory.getInventoryCnt());
        //1. delete cache
        inventoryService.removeProductInventoryCache(productInventory);
        // 为了模拟演示先删除了redis中的缓存，然后还没更新数据库的时候，读请求过来了，这里可以人工sleep一下
		/*try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
        //2. update db
        inventoryService.updateProductInventory(productInventory);
    }

    @Override
    public Integer getProductId() {
        return productInventory.getProductId();
    }

    @Override
    public boolean isForceRefresh() {
        return false;
    }

    public ProductUpdateRequest(ProductInventory inventory) {
        this.productInventory = inventory;
    }

    public ProductUpdateRequest(ProductInventory inventory, ProductInventoryService inventoryService) {
        this(inventory);
        this.inventoryService = inventoryService;
    }
}
